﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;

namespace SEO.Spider
{
    /// <summary>
    /// 版权所有: 版权所有(C) 2011，上海云里网络科技有限公司
    /// 内容摘要: 智联招聘网采集类
    /// 完成日期：2011年9月15日
    /// 版    本：V1.0 
    /// 作    者：Adin
    /// </summary>
    public class ESZhaoPin
    {

        /// <summary>
        /// 根据规则抓取新闻列表页面内容
        /// </summary>
        /// <param name="strWebUrl">网址</param>
        /// <param name="strStartMarkString">开始标记</param>
        /// <param name="strEndMarkString">结束标记</param>
        /// <param name="rb">显示实时消息控件</param>
        /// <param name="strMainChannelID">文章主分类ID</param>
        /// <param name="strChannelID">文章子分类ID</param>
        /// <param name="strWebName">网站频道名称</param>
        /// <param name="iCatchID">抓取地址ID</param>
        /// <param name="strWebCharset">网页编码</param>
        public static void AccordingURLGetNewsList(string strWebUrl, string strStartMarkString, string strEndMarkString, System.Windows.Forms.RichTextBox rb, string strMainChannelID, string strChannelID, string strWebName, int iCatchID,string strWebCharset)
        {
            try
            {
                SEO.Utility.WebClient wc = new SEO.Utility.WebClient();
                wc.Encoding = System.Text.Encoding.GetEncoding(strWebCharset);
                wc.OpenRead(strWebUrl);
                string strGetHtmlCode = wc.RespHtml;

                //匹配列表源代码
                Regex rx = new Regex(strStartMarkString + "(?<content>[\\s\\S]*)?" + strEndMarkString, RegexOptions.Singleline);
                string strGetListHtmlContent = rx.Match(strGetHtmlCode).Result("${content}");

                GetNewsListAllLink(strGetListHtmlContent, rb, strMainChannelID, strChannelID, strWebName, strWebCharset);
            }
            catch (Exception ex)
            {
                string strError = "文章列表抓取匹配失败，主分类ID：" + strMainChannelID + "，子分类ID：" + strChannelID + "\r\n" + ex.Message;

                SEO.Utility.PublicValue.SaveMessage(Convert.ToDateTime(DateTime.Now).ToString("MM-dd HH:mm:ss") + " 【" + strWebName + "】列表地址抓取失败！");
                rb.Text = SEO.Utility.PublicValue.GetMessage();
            }

        }

        /// <summary>
        /// 获取列表页面单篇文章链接
        /// </summary>
        /// <param name="strGetHtmlContent">匹配到的HTML代码</param>
        /// <param name="rb">显示实时消息控件</param>
        /// <param name="strMainChannelID">文章主分类ID</param>
        /// <param name="strChannelID">文章子分类ID</param>
        /// <param name="strChannelName">文章来源网站名称</param>
        /// <param name="strWebCharset">网页编码</param>
        public static void GetNewsListAllLink(string strGetHtmlContent, System.Windows.Forms.RichTextBox rb, string strMainChannelID, string strChannelID, string strChannelName, string strWebCharset)
        {
            try
            {
                Regex clearString = new Regex("<td id=\"dvCompNM\"><a href=\"(.+?)\" target=\"_blank\"", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                Match m = null;
                for (m = clearString.Match(strGetHtmlContent); m.Success; m = m.NextMatch())
                {
                    //匹配到链接地址
                    string strLink = m.Groups[1].ToString().Trim();

                    #region
                    
                    SEO.Utility.PublicValue.SaveMessage(Convert.ToDateTime(DateTime.Now).ToString("MM-dd HH:mm:ss") + " 正在采集链接 -> " + strLink);
                    rb.Text = SEO.Utility.PublicValue.GetMessage();

                    //写入数据
                    SEO.Utility.IniFile iniFile = new Utility.IniFile(System.Windows.Forms.Application.StartupPath + "\\config.ini");
                    iniFile.WriteValue("CATCH", "LINK", strLink);
                    iniFile.WriteValue("CATCH", "MID", strMainChannelID);
                    iniFile.WriteValue("CATCH", "CID", strChannelID);
                    iniFile.WriteValue("CATCH", "CNAME", strChannelName);
                    iniFile.WriteValue("CATCH", "CHARSET", strWebCharset);

                    string strStatus = "";
                    bool bValue = false;
                    int iOverTime = int.Parse(iniFile.GetString("CATCH", "OVERTIME", ""));//读取超时时间，默认10秒=100000
                    bValue = SEO.Utility.OverTimeCntrol.CallFuncThread(GetArticlesContentAccordingLink, TimeSpan.FromMilliseconds(iOverTime), null);

                    if (!bValue)
                    {
                        strStatus = "读取网址超时(" + iOverTime / 1000 + "秒)，跳过！";
                    }
                    else
                    {
                        int iNum = int.Parse(iniFile.GetString("CATCH", "STATUSCODE", ""));

                        if (iNum == 1)
                        {
                            strStatus = "匹配电子邮件失败";
                        }
                        if (iNum == 2)
                        {
                            strStatus = "电子邮件匹配成功";
                        }
                        if (iNum == 3)
                        {
                            strStatus = "重复，跳过";
                            //break;
                        }
                        if (iNum == 4)
                        {
                            strStatus = "加入失败";
                        }
                        if (iNum == 5)
                        {
                            strStatus = "匹配异常";
                        }
                        if (iNum == 6)
                        {
                            strStatus = "邮箱为空";
                        }

                    }

                    SEO.Utility.PublicValue.SaveMessage("\r\n" + DateTime.Now.ToString("MM-dd HH:mm:ss") + " 状态：" + strStatus);
                    rb.Text = SEO.Utility.PublicValue.GetMessage();

                    #endregion

                }
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("获取网页地址列表 ->" + ex.Message);
            }
        }

        /// <summary>
        /// 根据文章链接获取文章对应的内容，并添加到数据库中
        /// </summary>
        public static void GetArticlesContentAccordingLink()
        {
            SEO.Utility.IniFile iniFile = new Utility.IniFile(System.Windows.Forms.Application.StartupPath + "\\config.ini");
            int iNum = 1;
            try
            {
                
                string strLink = iniFile.GetString("CATCH", "LINK", "");
                string strMainChannelID = iniFile.GetString("CATCH", "MID", "");
                string strChannelID = iniFile.GetString("CATCH", "CID", "");
                string strChannelName = iniFile.GetString("CATCH", "CNAME", "");
                string strCharset = iniFile.GetString("CATCH", "CHARSET", "");

                //抓取网页源代码
                SEO.Utility.WebClient wc = new SEO.Utility.WebClient();
                wc.Encoding = System.Text.Encoding.GetEncoding(strCharset);
                wc.OpenRead(strLink);
                string strGetHtmlCode = wc.RespHtml;

                string strEmail = "";

                //匹配电子邮件
                try
                {
                    //匹配电子邮件
                    strEmail = Regex.Match(strGetHtmlCode, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*").ToString();
                }
                catch (Exception ex)
                {
                    SEO.Utility.Log.SaveErrorLog("匹配电子邮件失败,错误信息：" + ex.Message);
                    iNum = 1;
                }

                try
                {
                    SEO.DAL.ESEmailsDAL emaildal = new DAL.ESEmailsDAL();

                    if (!emaildal.CheckEmailAddressIsExist(strEmail))
                    {
                        if (string.IsNullOrEmpty(strEmail))
                        {
                            iNum = 6;//电子邮件为空
                        }
                        else
                        {
                            SEO.Model.ESEmails email = new Model.ESEmails();
                            email.EmailID = emaildal.GetAutoAddNum();
                            email.EmailAddress = strEmail;
                            email.Keywords = strChannelName;
                            email.EmailSource = strLink;
                            SEO.BLL.ESEmailsBLL emailbll = new BLL.ESEmailsBLL();
                            emailbll.InsertESEmailsBLL(email);

                            iNum = 2;//匹配成功
                        }
                    }
                    else
                    {
                        iNum = 3;//电子邮件重复
                    }
                }
                catch (Exception ex)
                {
                    SEO.Utility.Log.SaveErrorLog("插入数据库时失败,错误信息：" + ex.Message);
                    iNum = 4;
                }

            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("根据文章链接获取文章对应的内容，并添加到数据库中 -> " + ex.Message);
                iNum = 5;
            }

            iniFile.WriteValue("CATCH", "STATUSCODE", iNum);
        }

    }
}
